<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta content="IE=edge" http-equiv="X-UA-Compatible">
    <title>千锋健康</title>
    <meta content="千锋健康" name="description">
    <meta content="千锋健康" name="keywords">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link href="../plugins/elementui/index.css" rel="stylesheet">
    <link href="../plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <link href="../css/style.css" rel="stylesheet">
    <!-- 引入组件库 -->
    <script src="../js/jquery.min.js" type="text/javascript"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }

        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>预约管理<small>套餐管理</small></h1>
        <el-breadcrumb class="breadcrumb" separator-class="el-icon-arrow-right">
            <el-breadcrumb-item><a href="show.html">首页</a></el-breadcrumb-item>
            <el-breadcrumb-item>预约管理</el-breadcrumb-item>
            <el-breadcrumb-item>套餐管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <!--                        placeholder 默认显示字符 keyup.enter.native 回车事件-->
                <el-input @keyup.enter.native="handleFilter" class="filter-item" placeholder="编码/名称/助记码"
                          style="width: 200px;" v-model="pagination.queryString"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button @click="handleCreate()" class="butT" type="primary">新建</el-button>
            </div>
            <el-table :data="dataList" current-row-key="id" highlight-current-row size="small" stripe>
                <el-table-column align="center" label="序号" type="index"></el-table-column>
                <el-table-column align="center" label="套餐编码" prop="code"></el-table-column>
                <el-table-column align="center" label="套餐名称" prop="name"></el-table-column>
                <el-table-column align="center" label="适用性别">
                    <template slot-scope="scope">
                        <span>{{ scope.row.sex == '0' ? '不限' : scope.row.sex == '1' ? '男' : '女'}}</span>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="适用年龄" prop="age"></el-table-column>
                <el-table-column align="center" label="价格" prop="price"></el-table-column>
                <el-table-column align="center" label="助记码" prop="helpCode"></el-table-column>
                <el-table-column align="center" label="说明" prop="remark"></el-table-column>
                <el-table-column align="center" label="图片" prop="img">
                    <template slot-scope="scope">
                        <img :src="scope.row.imgUrl" class="avatar" v-if="scope.row.imgUrl">
                    </template>
                </el-table-column>
                <el-table-column align="center" label="操作">
                    <template slot-scope="scope">
                        <el-button @click="edit(scope.row)" size="mini" type="primary">编辑</el-button>
                        <el-button @click="del(scope.row.id)" size="mini" type="danger">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        :page-sizes="[3,5,10]"
                        :total="pagination.total"
                        @current-change="handleCurrentChange"
                        @size-change="handleSizeChange"
                        class="pagiantion"
                        layout="total,sizes, prev, pager, next, jumper">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog :visible.sync="dialogFormVisible" title="套餐表单">
                    <template>
                        <el-tabs type="card" v-model="activeName">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="编码">
                                                <el-input maxlength="4" oninput="value=value.replace(/[^\d]/g,'')" placeholder="请输入数字编码"
                                                          v-model="formData.code"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="名称">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="适用性别">
                                                <el-select v-model="formData.sex">
                                                    <el-option label="不限" value="0"></el-option>
                                                    <el-option label="男" value="1"></el-option>
                                                    <el-option label="女" value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="助记码">
                                                <el-input v-model="formData.helpCode"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="套餐价格">
                                                <el-input oninput="value=value.replace(/[^0-9.]/g,'').replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')"
                                                          v-model="formData.price"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="8">
                                            <el-form-item label="适用年龄">
                                                <template>
                                                    <el-select @change="preAgeChange" clearable placeholder="请选择"
                                                               v-model="formData.preAge">
                                                        <el-option
                                                                :key="item.value"
                                                                :label="item.label"
                                                                :value="item.value"
                                                                v-for="item in options">
                                                        </el-option>
                                                    </el-select>
                                                </template>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="8">
                                            <el-form-item label="——&emsp;&emsp;">
                                                <template>
                                                    <el-select @change="afterAgeChange" clearable placeholder="请选择"
                                                               v-model="formData.afterAge">
                                                        <el-option
                                                                :key="item.value"
                                                                :label="item.label"
                                                                :value="item.value"
                                                                v-for="item in options">
                                                        </el-option>
                                                    </el-select>
                                                </template>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="8"></el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="上传图片">
                                                <el-upload
                                                        :action="token"
                                                        :auto-upload="autoUpload"
                                                        :before-upload="beforeAvatarUpload"
                                                        :on-success="handleAvatarSuccess"
                                                        :show-file-list="false"
                                                        class="avatar-uploader"
                                                        name="imgFile">
                                                    <img :src="imageUrl" class="avatar" v-if="imageUrl">
                                                    <i class="el-icon-plus avatar-uploader-icon" v-else></i>
                                                </el-upload>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="说明">
                                                <el-input type="textarea" v-model="formData.remark"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="注意事项">
                                                <el-input type="textarea" v-model="formData.attention"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="检查组信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>项目编码</th>
                                            <th>项目名称</th>
                                            <th>项目说明</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" :value="c.id" type="checkbox" v-model="checkGroupIds">
                                            </td>
                                            <td><label :for="c.id">{{c.code}}</label></td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.remark}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div class="dialog-footer" slot="footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button @click="handleAdd()" type="primary" v-if="formButton=='add'">确定</el-button>
                        <el-button @click="handleEdit()" type="primary" v-if="formButton=='edit'">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    var vue = new Vue({
        el: '#app',
        data: {
            autoUpload: true,//自动上传
            imageUrl: null,//模型数据，用于上传图片完成后图片预览
            activeName: 'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 3,
                total: 100,
                queryString: null,
            },
            dataList: [],//列表数据
            //表单数据
            formData: {
                code: "",
                name: "",
                sex: -1,
                helpCode: "",
                price: 0,
                age: "",
                preAge: 0,
                afterAge: 0,
                remark: "",
                //添加表单窗口中检查组复选框对应id
                attention: "",
                checkGroupIds: []
            },//表单数据
            tableData: [],//添加表单窗口中检查组列表数据
            dialogFormVisible: false,//控制添加窗口显示/隐藏,
            checkGroupIds: [],
            options: [], //年龄选项的数组
            token: "",
            formButton: ""
        },
        created() {
            this.getToken();
            this.findPage();
        },
        methods: {
            //文件上传成功后的钩子，response为服务端返回的值，file为当前上传的文件封装成的js对象
            handleAvatarSuccess(response, file) {
                this.$message({
                    type: response.code == 200 ? "success" : "error",
                    message: response.message
                });
                if (response.code == 200) {
                    //预览刚刚上传的文件
                    this.imageUrl = "http://localhost:8080/setmeal/download?name=" + response.data + "&token=" + localStorage.getItem("security_token");
                    this.formData.img = response.data;//将文件名称赋值给模型数据，用于表单提交
                }
            },
            //上传图片之前执行
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 1021;
                if (!isJPG) {
                    this.$message.error('上传套餐图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传套餐图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            //添加
            handleAdd() {
                this.formData.checkGroupIds = this.checkGroupIds;
                let form = this.formData;
                console.log(form.code)
                //发送ajax请求，提交表单，包括两部分：套餐基本信息、关联的检查组id数组
                if (form.code != "") {
                    console.log("编码的长度是");
                    console.log(form.code.length);
                    if (form.code.length == 4) {
                        if (form.name != "") {
                            if (form.sex <= 2 || form.sex >= 0) {
                                if (form.helpCode != "") {
                                    if (form.price != 0) {
                                        if (form.preAge < form.afterAge) {
                                            if (form.checkGroupIds.length != 0) {
                                                axios({
                                                    url: "http://localhost:8080/setmeal/add",
                                                    method: "post",
                                                    data: this.formData,
                                                    headers: {
                                                        token: localStorage.getItem("security_token")
                                                    }
                                                }).then((result) => {
                                                    let code = result.data.code;
                                                    if (code == 200) {
                                                        this.$message(result.data.message);
                                                        this.dialogFormVisible = false;
                                                        this.findPage();
                                                    } else if (code == 401 || code == 504 || code == 505) {
                                                        this.$message.error(result.data.message);
                                                        location.href = "../login.html";
                                                    } else {
                                                        this.$message.error(result.data.message);
                                                    }
                                                });//axios结束
                                            } else {
                                                this.$message.error("套餐的检查项目不能为空");
                                            }
                                        } else {
                                            this.$message.error("请选择正确的年龄范围");
                                        }
                                    } else {
                                        this.$message.error("请输入正确的价格");
                                    }
                                } else {
                                    this.$message.error("助记码不能为空");
                                }
                            } else {
                                this.$message.error("请选择正确的适用性别");
                            }
                        } else {
                            this.$message.error("名称不能为空");
                        }
                    } else {
                        this.$message.error("编码须4位");
                    }
                } else {
                    this.$message.error("编码不能为空");
                }
            },
            //分页查询
            findPage() {
                let token = localStorage.getItem("security_token");
                axios({
                    url: "http://localhost:8080/setmeal/findpage",
                    method: "post",
                    data: this.pagination,
                    method: "post",
                    headers: {
                        token: token
                    }
                }).then((result) => {
                    if (result.data.code == 200) {
                        this.pagination.total = result.data.data.total;
                        let list = result.data.data.records;
                        console.log("查看list");
                        console.log(list);
                        for (let i = 0; i < list.length; i++) {
                            list[i].imgUrl="http://localhost:8080/setmeal/download?name=" + list[i].img + "&token=" + localStorage.getItem("security_token");
                        }
                        console.log(list);
                        this.dataList = list;
                        console.log(list)
                    } else if(result.data.code == 401 || result.data.code == 504 || result.data.code == 505){
                        this.$message.error(result.data.message);
                        location.href = "../login.html";
                    }else if(result.data.code == 403) {
                        this.$message.error(result.data.message);
                        location.href = "show.html";
                    }else {
                        this.$message.error(result.data.message);
                    }
                });
            },
            // 重置表单
            resetForm() {
                this.formData = {};
                this.activeName = 'first';
                this.checkGroupIds = [];
                this.imageUrl = null;
            },
            // 弹出添加窗口
            handleCreate() {
                //重置表单数据
                this.resetForm();
                //显示的按钮为提交按钮
                this.formButton = "add";
                this.dialogFormVisible = true;
                this.getOptions();
                this.getCheckGroup();
            },//点击新增弹窗事件结束
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;//指定最新的页码
                this.findPage();
            },
            //回车事件绑定查询按钮事件
            handleFilter() {
                this.findPage();
            },
            //页码size change事件
            handleSizeChange(pageSize) {
                this.pagination.pageSize = pageSize;//指定最新的页码
                this.findPage();
            },
            //非token 是全路径拼接token
            getToken() {
                this.token = "http://localhost:8080/setmeal/upload?token=" + localStorage.getItem("security_token");
            },
            //获取年龄选择的列表
            getOptions() {
                this.options = [];
                for (let i = 1; i <= 150; i++) {
                    this.options.push({value: i, label: i});
                }
            },
            //    preAge变动
            preAgeChange() {
                if (this.formData.afterAge > this.formData.preAge) {
                    this.formData.age = this.formData.preAge + "-" + this.formData.afterAge;
                } else if (this.formData.afterAge <= this.formData.preAge && this.formData.afterAge != 0) {
                    alert("请选择正确的范围");
                }
            },
            //    preAge变动
            afterAgeChange() {
                if (this.formData.afterAge > this.formData.preAge) {
                    this.formData.age = this.formData.preAge + "-" + this.formData.afterAge;
                } else {
                    alert("请选择正确的范围");
                }
            },
            //删除
            del(id) {
                if (confirm("确认删除？")) {
                    let token = localStorage.getItem("security_token");
                    axios({
                        url: "http://localhost:8080/setmeal/del",
                        method: "post",
                        data: {id: id},
                        headers: {
                            token: token
                        }
                    }).then((result) => {
                        if (result.data.code == 200) {
                            this.$message({
                                type: 'success',
                                message: result.data.message
                            });
                            this.findPage();
                        } else {
                            this.$message.error(result.data.message);
                            location.href = "../login.html";
                        }
                    });
                }
            },
            //    修改弹窗
            edit(row) {
                //基础框架
                this.resetForm();
                this.formButton = "edit";
                this.dialogFormVisible = true;
                this.getOptions();
                this.getCheckGroup();
                this.imageUrl =  "http://localhost:8080/setmeal/download?name=" + row.img + "&token=" + localStorage.getItem("security_token");
                //    赋vue.formData row数据
                //    把age字符分成两个数字
                let ages = row.age.split('-');
                let preAge = ages[0];
                let afterAge = ages[1];
                let form = {
                    id: row.id,
                    code: row.code,
                    name: row.name,
                    sex: row.sex,
                    preAge: preAge,
                    afterAge: afterAge,
                    price: row.price,
                    helpCode: row.helpCode,
                    remark: row.remark,
                    attention: row.attention,
                    checkGroupIds: [],
                    img: row.img
                }
                //    查询id对应的gid数组赋进vue的gid数组
                let token = localStorage.getItem("security_token");
                axios({
                    url: "http://localhost:8080/setmeal/getcheckgroupids",
                    method: "get",
                    params: {
                        id: form.id
                    },
                    headers: {
                        token: token
                    }
                }).then((result) => {
                    if (result.data.code == 200) {
                        let list = result.data.data;
                        console.log("查看返回的groupId数组")

                        this.checkGroupIds = list;
                        console.log(this.tableData);
                    } else {
                        this.$message.error(result.data.message);
                        location.href = "../login.html";
                    }
                });
                //    this.formData=该对象
                this.formData = form;
            },
            //修改提交
            handleEdit() {
                this.formData.checkGroupIds = this.checkGroupIds;
                let form = this.formData;
                //发送ajax请求，提交表单，包括两部分：套餐基本信息、关联的检查组id数组
                if (form.code != "") {
                    if (form.name != "") {
                        if (form.sex <= 2 || form.sex >= 0) {
                            if (form.helpCode != "") {
                                if (form.price != 0) {
                                    if (form.preAge < form.afterAge) {
                                        if (form.checkGroupIds.length != 0) {
                                            axios({
                                                url: "http://localhost:8080/setmeal/edit",
                                                method: "post",
                                                data: this.formData,
                                                headers: {
                                                    token: localStorage.getItem("security_token")
                                                }
                                            }).then((result) => {
                                                let code = result.data.code;
                                                if (code == 200) {
                                                    this.$message({type: "success", message: result.data.message});
                                                    this.dialogFormVisible = false;
                                                    this.findPage();
                                                } else if (code == 401 || code == 504 || code == 505) {
                                                    this.$message.error(result.data.message);
                                                    // location.href = "../login.html";
                                                } else {
                                                    this.$message.error(result.data.message);
                                                }
                                            });//axios结束
                                        } else {
                                            this.$message.error("套餐的检查项目不能为空");
                                        }
                                    } else {
                                        this.$message.error("请选择正确的年龄范围");
                                    }
                                } else {
                                    this.$message.error("请输入正确的价格");
                                }
                            } else {
                                this.$message.error("助记码不能为空");
                            }
                        } else {
                            this.$message.error("请选择正确的适用性别");
                        }
                    } else {
                        this.$message.error("名称不能为空");
                    }
                } else {
                    this.$message.error("编码不能为空");
                }
            },
            //封装 获取检查组
            getCheckGroup() {
                let token = localStorage.getItem("security_token");
                axios({
                    url: "http://localhost:8080/setmeal/getcheckgroup",
                    method: "get",
                    headers: {
                        token: token
                    }
                }).then((result) => {
                    if (result.data.code == 200) {
                        let list = result.data.data;
                        this.tableData = list;
                    } else {
                        this.$message.error(result.data.message);
                        location.href = "../login.html";
                    }
                });
            }
        }//methods结束
    })
</script>
</html>
